﻿<?php
include('controller/poi.inc.php');
class admin extends poi
{
public function title()
{
	return 'Admin';
}

	function indexAction($db)
	{
      $this->database = $db;
      global $action;
         switch( $action)
         {
            case 'formAddPoints':
               include('formAddPoints.php');
               break;
           case 'formAddCat':
                  include('formAddCat.php');
                  break;
           case 'formAddFile':
                  include('formAddFile.php');
                  break;
            case 'formSearch':
               include('formSearch.php');
                  break;
           case 'addPoints':
                $this->addPoints();
                echo 'Poprawnie dodano punkt.';
               break;
            case 'addFile':
                  $this->addFile();
                  break;
            case 'addUCat':
                  include('showUcat.php');
                  $this->addUCat();
                  echo 'Dodano kategorię.';
                  break;
            case 'addCat':
               $this->addCat();
               echo 'Dodano kategorię.';
               break;
            case 'logout';
               break;
            case 'search':
               include('formSearch.php');
               include('showSearchResult.php');
               break;
            case 'xml':
            case 'export':
                if(isset($_POST['kml']))
                {
                    $this->exportKml();
                }
                if(isset($_POST['gpx']))
                {
                    $this->exportGpx();
                }
                 if(isset($_POST['addToList']))
                {
                    $this->addToList();
                }
                if(isset($_POST['xslt']))
                {
                    $this->kmlXslt();
                }
                break;
            case 'showAllLists':
               include('showAllLists.php');
            break;
             case 'xsl':
               include('formXSL.php');
                break;
            case 'gpx':
               $this->exportGpx();
               break;
            default :
               include('showAllPoints.php');
               break;
         }
	}

function resize($filename )
{
   //ECHO basename  ($filename);
   list($width, $height) = getimagesize($filename);
   if(($width > 320) || ($height > 240))
   {
      if($width > $height)
      $raito = 320/$width;
      else
      $raito = 240/$height;

      $newwidth = $raito*$width;
      $newheight = $raito*$height;
      $thumb = imagecreatetruecolor($newwidth, $newheight);

      $ext = substr($filename, strlen($filename)-3,3 );
      switch ($ext)
      {

      case "GIF":
          case "gif":
         $source = imagecreatefromgif($filename);
         break;
      case "jpg":
          case "JPG":
         $source = imagecreatefromjpg($filename);
         break;
      case "PNG":
      case "png":
         $source = imagecreatefrompng($filename);
         break;
      default:
         $source = imagecreatefromjpg($filename);
         break;
   }
   imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
   imagejpeg($thumb, $filename, 100);
}
}

function addFile()
{
   $typy = array('image/gif', 'image/jpeg', 'image/png', 'image/jpg');
   if (is_uploaded_file($_FILES['plik']['tmp_name']))
   {
      if(!in_array($_FILES['plik']['type'], $typy, true))
      {
         echo "Nie dozwolony format pliku!";
         exit;
      }
      else
      {
         if($_FILES['plik']['size']<256000 && $_FILES['plik']['size']>1  )
         {
            $plik_tmp = $_FILES['plik']['tmp_name'];
            $plik_nazwa = $_FILES['plik']['name'];
            $plik_rozmiar = $_FILES['plik']['size'];
            move_uploaded_file($plik_tmp, "upload/$plik_nazwa");
            echo "Plik: <strong>$plik_nazwa</strong> o rozmiarze
           <strong>$plik_rozmiar bajtów</strong> został przesłany na serwer!";
            $this->resize("upload/$plik_nazwa");
         }
      }
   }
}

function addUCat()
{
   $nazwa = mysql_real_escape_string( $_POST['nazwa'] );
   mysql_query("INSERT INTO ucat SET name = '$nazwa' ");
   //$zid = mysql_insert_id();
}

function addCat()
{
   $nazwa = mysql_real_escape_string( $_POST['nazwa'] );
   $ucat = $_POST['kategoria'];
   mysql_query("INSERT INTO cat SET name = '$nazwa', id_ucat = (SELECT ID FROM `ucat` WHERE `name` = '$ucat' )");
}
}
?>